<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 
 * 后台模块控制器类
 * @author 66diqiu-li
 *
 */
class Admin_Modules_module extends CI_Module {
    
    //模块所处目录路径
    protected $appPath = '';
    //已安装模块列表
    protected $moduleList = array();
    //系统模块，隐藏
    protected $systemModuleList = array('demo', 'admin', 'install', 'cloud', 'member', 'index');
    
    /**
     * 
     * 构造函数
     */
    public function __construct(){
        parent::__construct();
        $this->appPath = getcwd() . '/'.MODPATH;        
    }
	
    /**
     * 
     * 本地模块管理列表
    */
    public function index(){
	
        $this->load->model('admin.Modules_model');

        //已安装模块
        $this->moduleList = $this->Modules_model->get_list();

        //取得模块目录名称
        $dirs = glob($this->appPath . '*');
        foreach ($dirs as $path) 
        {
            if (is_dir($path)) 
            {
                //目录名称
                $path = basename($path);
                //系统模块隐藏
                if (in_array($path, $this->systemModuleList)) {
                    continue;
                }
                $dirs_arr[] = $path;
            }
        }

        //取得已安装模块列表
        $moduleList = array();
        foreach ($this->moduleList as $v)
        {
            $moduleList[$v['module']] = $v;
            //检查是否系统模块，如果是，直接不显示
            if ($v['iscore'])
            {
                $key = array_keys($dirs_arr, $v['module']);
                if(isset($key[0]))
                {
                    unset($dirs_arr[$key[0]]);
                }
            }
        }
        
        $page_config['perpage']=50;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']='admin/modules/index';//url
        $page_config['seg']=4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
        $page_config['total'] = count($dirs_arr);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);    
        
        //把一个数组分割为新的数组块
        $dirs_arr = array_chunk($dirs_arr, $page_config['perpage'], true);
        
        //根据分页取到对应的模块列表数据
        $directory = $dirs_arr[intval($page_config['nowindex'] - 1)];
        $moduleList = array();
        foreach ($directory as $module) {
            $moduleList[$module] = $this->Modules_model->config($module);
            $moduleList[$module]['install_url'] = site_url('admin/modules/install?name='.$module);
            $moduleList[$module]['uninstall_url'] = site_url('admin/modules/uninstall?name='.$module);
        }
        //返回数据
        $data = array();
        $data['module_list'] = $moduleList;
        $this->load->view('modules_list',$data);
    }
	
    /**
     * 
     * 模块安装
     */
    public function install(){
        if(isset($_GET['name'])){
            $module = $_GET['name'];
            if(empty($module)) {
                $this->error('请选择需要安装的模块!','',true);
            }
            $this->load->model('admin.Modules_model');
            if($this->Modules_model->install($module)){
            	$this->success('模块安装成功!',site_url('admin/modules/index'),true);
            }else{
                $error = $this->Modules_model->error;
                $this->error($error?$error:'模块安装失败！','',true);
            }
        }
    }
	
    /**
     * 
     * 模块卸载
     */
    public function uninstall(){
        if(isset($_GET['name'])){
            $module = $_GET['name'];
            if(empty($module)) {
                $this->error('请选择需要卸载的模块!','',true);
            }
            $this->load->model('admin.Modules_model');
            if($this->Modules_model->uninstall($module)){
                $this->success('模块卸载成功!',site_url('admin/modules/index'),true);
            }else{
                $error = $this->Modules_model->error;
                $this->error($error?$error:'模块卸载失败！','',true);
            }
        }	    
    }
}